// 引入数据库
let mysql=require('mysql');
// 创建一个mysql连接对象，里面设置好连接数据库的配置
let connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    database : 'blog'
  });
//   连接数据库
connection.connect();
module.exports={
    // 文章的添加
    addArticle:(newArticle,callback)=>{
        let sqlStr="insert into articles set ?"
        connection.query(sqlStr,newArticle,(err,result)=>{
            if(err) return callback(err,null)
            callback(null,result)
            // console.log(result);
            
        })
    },

    // 文章详情
    articleDetail:(articleId,callback)=>{
        let sqlStr='select articles.*, users.nickname from articles LEFT JOIN users ON articles.userId=users.id WHERE articles.id=?';
        connection.query(sqlStr,articleId,(err,result)=>{
            if(err) return callback(err,null)
            callback(null,result                                      )
        })
    },
    // 文章编辑
    articleEdit:(articleId,callback)=>{
        let sqlStr="select * from articles where id=?";
        connection.query(sqlStr,articleId,(err,result)=>{
            if(err) return callback(err,null)
            callback(null,result)
        })
    },
    articleUpdate:(article,callback)=>{
        let sqlStr="update articles set title=?, content=?, ctime=? where id=?";
        connection.query(sqlStr,[article.title, article.content, article.ctime, article.id],(err,result)=>{
            if(err) return callback(err,null)
            callback(null,result)
        })
    }
}